package edu.mis.modules.room;

import com.jfinal.core.Controller;

import edu.mis.model.Room;
import edu.mis.modules.user.UserService;

public class RoomController extends Controller{
	
	static RoomService service = new RoomService();
	static UserService userService = new UserService();
	
	public void index() {
		if(userService.findUserWithRole().equals(1)) {
			render("room.html");
		}else if(userService.findUserWithRole().equals(2) || userService.findUserWithRole().equals(3)) {
			render("noRoomAuthority.html");
			
		}else {
			render("noAllAuthority.html");
		}
	}
	
	public void list(){
		setAttr("data", service.findAllRoomWithCondition());
		renderJson();  
	}
	
	/**
	 * 添加房间视图
	 */
	public void add() {
		//render("add.html");  //（默认对应add.html，否则需显性指定，如render("addRole.html")）
	}
	
	public void save(){
		Room room  =  getModel(Room.class);
		if(room.getConditionId()==null) {
			room.setConditionId(1);
		}
		if(service.saveRoom(room)){
			setAttr("result", true); 
			setAttr("msg", "添加房间成功!"); 
		}else{
			setAttr("result",false);
			setAttr("msg", "添加房间失败，请重新添加!"); 
		}
		renderJson(); 
	}
	
	public void edit() {
		setAttr("room", service.findRoomByRoomId(getParaToInt("roomId")));  //此处room应与表单中${(room.name)!}等中的room保持一致。
	}
	
	public void update(){
		int roomId = getParaToInt("roomId");
		Room room = service.findRoomByRoomId(roomId);
		room.setRoomNumber(this.getParaToInt("roomNumber"));
		if(this.getParaToInt("conditionId")==null) {
			room.setConditionId(1);
		}else {
			room.setConditionId(this.getParaToInt("conditionId"));
		}
		room.setRemarks(this.getPara("remarks"));
		if(service.updateRoom(room)){
			setAttr("result", true);
			setAttr("msg", "修改成功!");
		}else{
			setAttr("result", false);
		}
		renderJson();
	}
	
	/**
	 * 删除房间
	 * 
	 */
	public void delete() {
		if(service.deleteRoomByRoomId(getParaToInt("roomId"))){
			setAttr("result", true);
			setAttr("msg", "删除房间成功!");
		}else{
			setAttr("result", false);
		}
		renderJson();
	}
}
